package org.brisling.oa.service;

import java.util.List;

import org.brisling.authorize.shiro.ShiroSecurityService;
import org.brisling.base.domain.ListDomain;
import org.brisling.base.domain.RetDomain;
import org.brisling.common.ConstSysParam;
import org.brisling.oa.domain.OADrugStatisticAudit;
import org.brisling.oa.queryService.QueryOADrugStatisticAuditService;
import org.brisling.oa.repository.OADrugStatisticAuditRepo;
import org.brisling.oa.service.base.BaseOaFlowService;
import org.brisling.workflow.domain.VOHistoric;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;


@RestController
@RequestMapping("/oaDrugStatisticAudit")
/**
 * 药品用量统计模块service
 * @Author : jackson wang
 * @Date   : 2016年1月25日 下午4:18:01
 * @version: 1.0
 */
public class OADrugStatisticAuditService extends BaseOaFlowService<OADrugStatisticAudit>{
	
	//业务模块仓库对象
	@SuppressWarnings("unused")
	@Autowired
	private OADrugStatisticAuditRepo baseFlowRepo;	
	
	//登录用户验证及用户信息服务类
	@Autowired
	private ShiroSecurityService shiroSecurityService;	
	
	/**
	 * 构造函数
	 */
	public OADrugStatisticAuditService(){
		//业务实体类
		this.clazz = OADrugStatisticAudit.class;
		
		//业务模块定义
		this.moduleName = ConstSysParam.MODULE_OA_DRUGSTATISTIC_AUDIT;		
	}
	
		
	/**
	 * 获取业务主页面 
	 * @return
	 */
	@RequestMapping("/mainPage.action")
	public ModelAndView getMainPage(){
		return getBizMainPage();
	}
	
	/**
	 * 获取业务编辑页面
	 * @return
	 */
	@RequestMapping("/editPage.action")
	public ModelAndView getEditPage(){
		return getBizEditPage();
	}
	
	/**
	 * 
	 */
	
	@RequestMapping("/flowHistorics.action")
	public List<VOHistoric> getHistoricByProcInstId(@RequestParam(value="flowInstId",required=false) String procInstId){
		return super.getHistoricByProcInstId(procInstId);
	}
	
	/**
	 * 返回缺陷记录流程实例id的执行记录
	 * @param procInstId 流程实例id
	 * @return List<HistoricTaskInstance> 流程实例执行记录
	 */
	@RequestMapping("/flowHistoricsListDomain.action")
	public ListDomain<VOHistoric> getBizHistoricListDomainByProcInstId(@RequestParam(value="flowInstId",required=false) String procInstId){
		return super.getHistoricListDomainByProcInstId(procInstId);
	}
	
	
	/**
	 * 删除业务数据及业务流程数据
	 * @param id	业务记录id
	 * @return
	 */
	@RequestMapping("/flowDel.action")
	public RetDomain delRow(@RequestParam(value="id",required=false) Long id,@RequestParam(value="ids",required=false) String ids){
		return super.delFlowRow(id, ids);
		
	}
	
	/**
	 * 保存业务数据
	 * @param body	业务数据参数
	 * @return
	 */
	@RequestMapping("/flowSave.action")
	public OADrugStatisticAudit save(@RequestBody String body){
		OADrugStatisticAudit obj = bodyToObj(body, OADrugStatisticAudit.class);	
		if(obj!=null){
			if(obj.getBizno()==null || obj.getBizno().trim().length()<=0){
				obj.setBizno(queryService.generateBizno());
			}
		}
		OADrugStatisticAudit domain= this.saveFlowEntity(shiroSecurityService.getCurrentUserid().toString(),obj);
		if((domain!=null) && (domain.getId()!=null)){
			domain = queryService.getOne(domain.getId());
		}
		return domain;
	}
	
	/**
	 * 代码保存业务数据，用于后台自动发起任务
	 * @param userid	当前用户id
	 * @param obj		业务实体对象
	 * @return
	 */
	
	public OADrugStatisticAudit autoSave(String userid,OADrugStatisticAudit obj){
			
		OADrugStatisticAudit recipel= this.saveFlowEntity(userid,obj);
		if((recipel!=null) && (recipel.getId()!=null)){
			recipel = queryService.getOne(recipel.getId());
		}
		return recipel;
	}
	
	/**
	 * 签收业务记录
	 * @param body
	 * @return
	 */
	@RequestMapping("/flowSign.action")
	public ListDomain<Object> saveSingInfo(@RequestBody String body){
		
		
		return this.saveSignInfo(shiroSecurityService.getCurrentUserid().toString(), body);
	}
	
	
	//业务类查询服务
	@Autowired
	private QueryOADrugStatisticAuditService queryService;
	
	/**
	 * 节点任务完成方法
	 * @param body
	 * @return
	 */
	@RequestMapping("/flowTaskComplete.action")
	public String completeTask(@RequestBody String body){
		OADrugStatisticAudit recipel =this.save(body);
		if(recipel!=null){
			
			return super.completeTaskAndCandidateByEntity(
					shiroSecurityService.getCurrentUserid().toString(),
					recipel, 
					"签收");
		}
		
		return "500";
	}
	
	/**
	 * 节点回退方法
	 * @param body
	 * @returns
	 */
	@RequestMapping("/flowSendback.action")
	public RetDomain saveSendbackTask(@RequestBody String body){
		OADrugStatisticAudit recipel =this.save(body);
		if(recipel!=null){
			return super.saveSendBack(
					shiroSecurityService.getCurrentUserid().toString(), 
					recipel, null, null);
			
		}
		RetDomain domain = new RetDomain();
		domain.setErrorMsg("业务保存失败");
		return domain;
	}
	
	
}
